home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / networking / pgpuam / sources / tprefs.cp < prev    next >
Encoding:
Text File  |  2000-06-23  |  4.6 KB  |  201 lines

  1. //    TPrefs.cp - Preference Object  
  2. // 
  3. // Apple Macintosh Developer Technical Support
  4. // Written by:  Vinnie Moscaritolo
  5. //
  6. //  Copyright (work in progress)  Apple Computer, Inc All rights reserved.
  7. //
  8. // You may incorporate this sample code into your applications without
  9. // restriction, though the sample code has been provided "AS IS" and the
  10. // responsibility for its operation is 100% yours.  However, what you are
  11. // not permitted to do is to redistribute the source as "DSC Sample Code"
  12. // after having made changes. If you're going to re-distribute the source,
  13. // we require that you make it clear in the source that the code was
  14. // descended from Apple Sample Code, but that you've made changes.
  15. // 
  16.  
  17. #include <PLStringFuncs.h>
  18. #include <string.h>
  19. #include <Files.h>
  20.  
  21. #include "TPrefs.h"
  22. #include "TMacException.h"
  23.  
  24.  
  25. // ---------------------------------------------------------------------------
  26.     TPrefs::TPrefs() //   
  27. // ---------------------------------------------------------------------------
  28. //  
  29. {
  30.  
  31.     fCollection = NewCollection();
  32.     ThrowMacErrIfNil(fCollection, nilHandleErr);    
  33.     
  34.  }
  35.  
  36.  
  37. // ---------------------------------------------------------------------------
  38.     TPrefs::~TPrefs() //   
  39. // ---------------------------------------------------------------------------
  40. //  
  41.     DisposeCollection (fCollection);
  42.  
  43. }
  44.  
  45. // ---------------------------------------------------------------------------
  46. OSErr TPrefs::Set(     FourCharCode    tag, 
  47.                      SInt32             id,
  48.                     SInt32             itemSize,
  49.                     void *            itemData)
  50. // ---------------------------------------------------------------------------
  51. //  
  52.     return AddCollectionItem(fCollection, tag, id, itemSize, itemData);
  53.  
  54. }
  55.  
  56. // ---------------------------------------------------------------------------
  57. OSErr TPrefs::Get(     FourCharCode    tag, 
  58.                      SInt32             id,
  59.                     SInt32*         itemSize,
  60.                     void *            itemData)
  61. // ---------------------------------------------------------------------------
  62. //  
  63.     return GetCollectionItem(fCollection, tag, id, itemSize, itemData);
  64. }
  65.  
  66.  
  67.  
  68. // ---------------------------------------------------------------------------
  69. OSErr TPrefs::Read() //   
  70. // ---------------------------------------------------------------------------
  71. //  
  72.     FSSpec    fss;
  73.     OSErr    err;
  74.     
  75.      err = this->GetPrefSpec(&fss);
  76.      if(!err)
  77.      {
  78.         short     fileRefNum;
  79.         Size    eof;
  80.         
  81.         SInt32     ByteCount;
  82.         SInt32     size;
  83.     
  84.          FSpOpenDF (&fss,fsRdPerm,&fileRefNum);
  85.  
  86.         GetEOF (fileRefNum, &eof);
  87.     
  88.         size = sizeof (SInt32);
  89.         err = FSRead (fileRefNum ,&size, &ByteCount);
  90.         if(!err)
  91.         {
  92.             if(ByteCount + sizeof (SInt32) <= eof)  
  93.             {
  94.                 Handle buffer = NewHandle (ByteCount);
  95.                 if (!buffer) err = MemError ( );
  96.                  HLockHi (buffer);
  97.  
  98.                 size = ByteCount;
  99.                 err = FSRead (fileRefNum ,&size, *buffer);
  100.                 if(!err)
  101.                 {
  102.                     err = UnflattenCollectionFromHdl ( fCollection, buffer);
  103.                 }
  104.                 HUnlock (buffer);
  105.             }
  106.             else 
  107.                 err = eofErr;
  108.         }
  109.         
  110.         FSClose (fileRefNum);
  111.      }
  112.      return err;
  113. }
  114.  
  115.  
  116.  
  117. // ---------------------------------------------------------------------------
  118. OSErr TPrefs::CreatePrefFile(FSSpec *fss)   
  119. // ---------------------------------------------------------------------------
  120. //  
  121.     return ( FSpCreate (fss, '????' ,kPrefsFileType,smSystemScript) );
  122.  
  123. }
  124.  
  125.  
  126. // ---------------------------------------------------------------------------
  127. OSErr TPrefs::GetPrefSpec(FSSpec *fss)   
  128. // ---------------------------------------------------------------------------
  129. //  
  130.     FSSpec         prefsFSS;
  131.  
  132. // find the pref folder
  133.     ThrowIfMacErr( FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder, &prefsFSS.vRefNum, &prefsFSS.parID ));
  134.  
  135.     // make a file spec for the prefs file 
  136.     FSMakeFSSpec(prefsFSS.vRefNum, prefsFSS.parID , "\ppref file", fss);
  137.  
  138. // needs to be written to general case.
  139.     return (-1);
  140.  
  141. }
  142.  
  143. // ---------------------------------------------------------------------------
  144. void TPrefs::Write() //   
  145. // ---------------------------------------------------------------------------
  146. //  
  147.     FSSpec    fss;
  148.     OSErr    err;
  149.     short     fileRefNum;
  150.  
  151.  
  152.      if(fCollection )
  153.     {
  154.  
  155.          err = this->GetPrefSpec(&fss);
  156.  
  157.         if( err == fnfErr    )
  158.         {
  159.               err = this->CreatePrefFile(&fss);
  160.         }
  161.  
  162.         if(!err)
  163.         {
  164.  
  165.             Handle flattened = NewHandle(0);
  166.             SInt32 size;
  167.             SInt32 ByteCount;
  168.             
  169.             
  170.             ThrowIfMacErr( FSpOpenDF (&fss,fsWrPerm,&fileRefNum));
  171.  
  172.             err = FlattenCollectionToHdl(fCollection, flattened);
  173.             ByteCount = GetHandleSize(flattened);
  174.             
  175.             size = sizeof (SInt32);
  176.             FSWrite (fileRefNum, &size ,&ByteCount);
  177.             
  178.             size = ByteCount;
  179.             FSWrite (fileRefNum, &size ,*flattened);
  180.              
  181.              DisposeHandle(flattened);
  182.          
  183.               FSClose (fileRefNum);
  184.               
  185.             FlushVol (nil, fss.vRefNum);
  186.         }
  187.         
  188.     }
  189.  
  190.  
  191. }
  192.  
  193.  
  194.